window.onload = ini;

function ini() {
	// 保留下拉框中的值
	var objItemText = $("#styleValue").attr("value");
	var searchTypeVal=$("#searchTypeValue").attr("value");
	var dateBeginVal=$("#dateBeginValue").attr("value");
	var dateEndVal=$("#dateEndValue").attr("value");
	$("#style").attr("value", objItemText);
	$("#searchType").attr("value", searchTypeVal);
	$("#dateBegin").attr("value", dateBeginVal);
	$("#dateEnd").attr("value", dateEndVal);
	$('#dateEnd').hide();
	$('.date').simpleDatepicker();
	typeChange();
}

/**
 * 报表类型判断显示相应输入框
 */
function styleCheck(type) {
	var style = $("#style").val();
	if (style == 'line3D' || style == 'line') {
		$('#month').hide();
		$('#season').hide();
		$('.date').show();
		if (type == "season") {
			$('.date').hide();
		} else if(type=="year"){
			$('.date').hide();
		}
	} else {
		$('#dateEnd').hide();
	}
}

/**
 * 查询类型判断显示相应输入框
 */
function typeChange() {
	var type = $('#searchType').val();
	if (type == "year") {
		$('#month').hide();
		$('#season').hide();
		$('.date').hide();
	} else if (type == 'day') {
		$('#month').hide();
		$('#season').hide();
		$('.date').show();
	} else if (type == 'month') {
		$('#month').show();
		$('#season').hide();
		$('.date').hide();
	} else if (type == 'season') {
		$('#month').hide();
		$('#season').show();
		$('.date').hide();
	}
	styleCheck(type);
}

/**
 * 判断类型,并根据类型设置参数
 */
function subcheck() {
	var type = $('#searchType').val();
	var style = $("#style").val();
	var now = new Date();
	var year = now.getFullYear();
	if (type == 'day') {
		// 柱状图查询某一天销售情况
		if (style == 'bar' || style == 'bar3D') {
			$('#dateEnd').attr('value', $('#dateBegin').val());
		}
	} else if (type == 'month') {
		if (style == 'bar' || style == 'bar3D') {
			var monthLast = getMonthLastDay(year, $('#month').val());
			// 起始时间
			var dateBegin = year + '-' + ($('#month').val()) + '-1';
			// 结束时间
			var dateEnd = year + '-' + ($('#month').val()) + '-' + monthLast;
			$('#dateBegin').attr('value', dateBegin);
			$('#dateEnd').attr('value', dateEnd);
		}
	} else if (type == 'season') {
		if (style == 'bar' || style == 'bar3D') {
			var season = $('#season').val();
			// 开始月份
			var monthBegin = (season - 1) * 3 + 1;
			// 结束月份
			var monthEnd = season * 3;
			// 获取结束月份最后一天
			var monthLast = getMonthLastDay(year, monthEnd);
			var dateBegin = year + '-' + monthBegin + '-1';
			var dateEnd = year + '-' + monthEnd + '-' + monthLast;
			$('#dateBegin').attr('value', dateBegin);
			$('#dateEnd').attr('value', dateEnd);
		}
	}
	return true;
}

/**
 * 获取月份最后一天
 * 
 * @param {}
 *            year
 * @param {}
 *            month
 * @return {}
 */
function getMonthLastDay(year, month) {
	var Nowdate = new Date();
	var MonthNextFirstDay = new Date(year, month, 1);
	var MonthLastDay = new Date(MonthNextFirstDay - 86400000);
	return MonthLastDay.getDate();
}
